﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using ChanExt;
using C.Unite;
using C.Entities;
using MySoft.Data;

namespace ArtLand.OA.Project.SystemMgr
{
    public partial class SysModelPer : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnPer.OnClientClick = Grid1.GetNoSelectionAlertReference("至少选择一项！");
                btnNew.OnClientClick = Window1.GetShowReference("SysModelPerChange.aspx", "新增权限");

                BindGrid();
            }
        }

        #region BindGrid

        private void BindGrid()
        {

            // 1.设置总项数
            Grid1.RecordCount = TbsDbSession.Default.From<Sys_ModPermis>().Count();
            List<Sys_ModPermis> items = TbsDbSession.Default.From<Sys_ModPermis>().OrderBy(Sys_ModPermis._.Orderid.Asc).ToList();
            // 3.绑定到Grid
            Grid1.DataSource = items;
            Grid1.DataBind();
            if (!string.IsNullOrEmpty(Request["mid"]))
            {
                List<string> stringlist = StringHelper.GetStrArray(Request["mid"].ToString(), ',');
                if (stringlist.Count == 1)
                {
                    string mid = stringlist[0];
                    List<int> intlist = new List<int>();
                    for (int i = 0; i < Grid1.Rows.Count; i++)
                    {
                        int ct = TbsDbSession.Default.From<Sys_ModToPer>().Where(Sys_ModToPer._.Mid == int.Parse(mid) && Sys_ModToPer._.Perid == int.Parse(Grid1.DataKeys[i][0].ToString())).Count();
                        if (ct > 0)
                        {
                            intlist.Add(i);
                        }
                    }
                    Grid1.SelectedRowIndexArray = (int[])intlist.ToArray();//(new int[intlist.size()])；.;
                }
            }
            else
            {
                btnPer.Visible = false;
                ToolBarS1.Visible = false;
                Grid1.EnableCheckBoxSelect = false;
            }
        }


        protected string GetEditUrl(object id)
        {
            return "javascript:" + Window1.GetShowReference("SysModelMgrChange.aspx?id=" + id);
        }

        protected void Grid1_PageIndexChange(object sender, ChanExt.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }


        #endregion

        #region Events
        protected void btnKey_Click(object sender, EventArgs e)
        {
            string n = "";
            List<string> stringlist = StringHelper.GetStrArray(Request["mid"].ToString(), ',');
            foreach (string stringmid in stringlist)
            {
                List<int> rowlist = new List<int>();
                foreach (int row in Grid1.SelectedRowIndexArray)
                {
                    n += row.ToString();
                    rowlist.Add(row);
                    int c = TbsDbSession.Default.From<Sys_ModToPer>()
                        .Where(Sys_ModToPer._.Mid == int.Parse(stringmid) && Sys_ModToPer._.Perid == int.Parse(Grid1.DataKeys[row][0].ToString()))
                        .Count();
                    if (c == 0)
                    {
                        Sys_ModToPer tmp = new Sys_ModToPer();
                        tmp.DeAttach();
                        tmp.Mid = int.Parse(stringmid);
                        tmp.Perid = int.Parse(Grid1.DataKeys[row][0].ToString());
                        TbsDbSession.Default.Insert<Sys_ModToPer>(tmp);
                    }
                }
                for (int i = 0; i < Grid1.Rows.Count; i++)
                {
                    if (rowlist.IndexOf(i) == -1)
                    {
                        //n += Grid1.DataKeys[i][0].ToString();
                        int perid = int.Parse(Grid1.DataKeys[i][0].ToString());
                        TbsDbSession.Default.Delete<Sys_ModToPer>(Sys_ModToPer._.Mid == int.Parse(stringmid) && Sys_ModToPer._.Perid == perid);
                    }
                }
            }
            //Alert.Show(n);
            Alert.ShowInTop("配置成功！");
            PageContext.RegisterStartupScript(ActiveWindow.GetHideReference());
        }
        //protected void btnDelete_Click(object sender, EventArgs e)
        //{
        //    int returnvalue = 0;
        //    bool bl = true;
        //    foreach (int row in Grid1.SelectedRowIndexArray)
        //    {
        //        int id=int.Parse(Grid1.DataKeys[row][0].ToString());
        //        int c=TbsDbSession.Default.From<Sys_Modules>().Where(Sys_Modules._.Topmid==id).Count();
        //        if (c == 0)
        //        {
        //            returnvalue += TbsDbSession.Default.Delete<Sys_Modules>(Sys_Modules._.Mid == id);
        //        }
        //        else
        //        {
        //            bl = false;
        //        }

        //    }
        //    if (returnvalue > 0)
        //    {
        //        if (bl == false)
        //        {
        //            Alert.ShowInTop("删除成功！但有些模块还存在子模块！");
        //            BindGrid();
        //        }
        //        else
        //        {
        //            Alert.ShowInTop("删除成功！");
        //            BindGrid();
        //        }
        //    }

        //}


        #endregion
        protected void Grid1_RowDataBound(object sender, ChanExt.GridRowEventArgs e)
        {


        }

        protected void Grid1_RowCommand(object sender, ChanExt.GridCommandEventArgs e)
        {
            int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
            if (e.CommandName == "Del")
            {
                TbsDbSession.Default.Delete<Sys_ModPermis>(Sys_ModPermis._.Perid == id);
                Alert.ShowInTop("删除成功！");
                BindGrid();

            }
        }
    }
}